// 引入pdf
import jsPDF from './jspdf'

//设置cookie的值
export function setCookie(name, value, Hours) {
    var d = new Date();
    var offset = 8;
    var utc = d.getTime() + d.getTimezoneOffset() * 60000;
    var nd = utc + 3600000 * offset;
    var exp = new Date(nd);
    exp.setTime(exp.getTime() + Hours * 60 * 60 * 1000);
    document.cookie =
      name +
      "=" +
      escape(value) +
      ";path=/;expires=" +
      exp.toGMTString() +
      ";domain=360doc.com;";
}
//获取cookie的值
export function getCookie(name) {
    var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
    if (arr != null) return unescape(arr[2]);
    return null;
}

//到处excel
export function tableToExcel(jsonData,header){

    //列标题，逗号隔开，每一个逗号就是隔开一个单元格
    let str = `${header}\n`;
    //增加\t为了不让表格显示科学计数法或者其他格式
    for(let i = 0 ; i < jsonData.length ; i++ ){
      for(let item in jsonData[i]){
          str+=`${jsonData[i][item] + '\t'},`;
      }
      str+='\n';
    }
    //encodeURIComponent解决中文乱码
    let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
    //通过创建a标签实现
    let link = document.createElement("a");
    link.href = uri;
    //对下载的文件命名
    link.download =  "体检科列表";
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
}

export function markIdCard(idCard){
  if(idCard){
    // 默认idCard的格式是符合规范的
    let len = idCard.length
    return idCard.slice(0,3)+'*'.repeat(len - 7)+idCard.slice(-4)
  }

}

export function markPhoneNumber(phoneNumber){
  if(phoneNumber){
    let len = phoneNumber.length
    return phoneNumber.slice(0,3)+'*'.repeat(len - 6)+phoneNumber.slice(-3)
  }

}


// 获取1毫米等于多少像素值
export function getMm2px() {
  var arrDPI = new Array();
  if ( window.screen.deviceXDPI != undefined ) {
      arrDPI[0] = window.screen.deviceXDPI;
      arrDPI[1] = window.screen.deviceYDPI;
  }
  else {
      var tmpNode = document.createElement( "DIV" );
      tmpNode.style.cssText = "width:1mm;height:1mm;position:absolute;left:0px;top:0px;z-index:99;visibility:hidden";
      document.body.appendChild( tmpNode );
      arrDPI[0] = parseInt( tmpNode.offsetWidth );
      arrDPI[1] = parseInt( tmpNode.offsetHeight );
      tmpNode.parentNode.removeChild( tmpNode );
  }
  // 只去宽度放心的dpi
  return arrDPI[0];
}


// 根据屏幕分辨率，获取a4纸像素值,以宽度为基准，高度等比例自适应
export function getA4px(){
  let a4Width_mm = 210
  let a4Height_mm = 297
  let mm2px = getMm2px()
  return [a4Width_mm * mm2px, a4Height_mm * mm2px]
}



// 将canvas转为pdf

export function canvasToPdf(canvas){

  var marginHeight = 20
  var marginWidth = 20
  var a4Width = 592.28
  var a4Height = 841.89
  var contentWidth = canvas.width;
  var contentHeight = canvas.height;


  //一页pdf显示html页面生成的canvas高度;
  var pageHeight = contentWidth / a4Width * a4Height;
  //未生成pdf的html页面高度
  var leftHeight = contentHeight;
  //页面偏移
  var position = 0;
  //a4纸的尺寸[595.28,841.89]，html页面生成的canvas在pdf中图片的宽高
  var imgWidth = a4Width;
  var imgHeight = a4Width/contentWidth * contentHeight;

  var pageData = canvas.toDataURL('image/jpeg', 1.0);

  var pdf = new jsPDF('', 'pt', 'a4');

  //有两个高度需要区分，一个是html页面的实际高度，和生成pdf的页面高度(841.89)
  //当内容未超过pdf一页显示的范围，无需分页
  if (leftHeight < pageHeight) {
    pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight );
  } else {
    while(leftHeight > 0) {
        pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
        leftHeight -= pageHeight;
        position -= a4Height;
        //避免添加空白页
        if(leftHeight > 0) {
          pdf.addPage();
        }
    }
  }
  pdf.save('风险评估报告.pdf');


}
